package org.mango.dsaa.demo;

import java.util.*;

/**
 * 运行结果，vector add了16次，最后长度16
 * t1 start
 * t1 Vector size is 1
 * t2 start
 * t2 Vector size is 2
 * t3 start
 * t3 Vector size is 3
 * t4 start
 * t4 Vector size is 4
 * t1 Vector size is 5
 * t2 Vector size is 6
 * t4 Vector size is 7
 * t3 Vector size is 8
 * t1 Vector size is 9
 * t4 Vector size is 10
 * t2 Vector size is 11
 * t3 Vector size is 12
 * t1 Vector size is 13
 * t4 Vector size is 15
 * t2 Vector size is 15
 * t3 Vector size is 16
 * 长度=16
 *
 * -------- arrayList add 了16次，长度只有13，线程不安全
 *
 * t2 start
 * t1 start
 * t2 ArrayList size is 1
 * t1 ArrayList size is 2
 * t3 start
 * t3 ArrayList size is 3
 * t4 start
 * t4 ArrayList size is 4
 * t1 ArrayList size is 6
 * t4 ArrayList size is 6
 * t3 ArrayList size is 7
 * t2 ArrayList size is 6
 * t4 ArrayList size is 8
 * t2 ArrayList size is 9
 * t1 ArrayList size is 8
 * t3 ArrayList size is 10
 * t2 ArrayList size is 12
 * t3 ArrayList size is 12
 * t4 ArrayList size is 11
 * t1 ArrayList size is 13
 * 长度=13
 *
 * ------ linkedList 加了16次，长度11，线程不安全
 *
 * t1 start
 * t3 start
 * t3LinkedList size is 2
 * t1LinkedList size is 2
 * t2 start
 * t4 start
 * t4LinkedList size is 4
 * t2LinkedList size is 3
 * t3LinkedList size is 6
 * t1LinkedList size is 5
 * t2LinkedList size is 7
 * t4LinkedList size is 7
 * t3LinkedList size is 8
 * t1LinkedList size is 8
 * t4LinkedList size is 9
 * t2LinkedList size is 9
 * t3LinkedList size is 10
 * t1LinkedList size is 10
 * t2LinkedList size is 11
 * t4LinkedList size is 11
 * 长度=11
 */
public class ArrayListVsVectorDemo {
    public static void main(String[] args) {
        //List v1 = new ArrayList();
        List v1 = new LinkedList();
        //List v1 = Collections.synchronizedList(new ArrayList());
        //List v1 = Collections.synchronizedList(new LinkedList());
        //List v1 = new Vector();
        TestListThread t1 = new TestListThread("t1",v1);
        TestListThread t2 = new TestListThread("t2",v1);
        TestListThread t3 = new TestListThread("t3",v1);
        TestListThread t4 = new TestListThread("t4",v1);
        t1.start();t2.start();t3.start();t4.start();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("长度="+v1.size());
    }
}

class TestListThread extends Thread{
    private String name;
    private List v;

    public TestListThread(String name,List v){
        this.name = name;
        this.v = v;
    }

    @Override
    public void run() {
        System.out.println(name + " start");
        int count = 4;
        while(count>0) {
            v.add(name + ".add");
            String typeName = "";
            if(v instanceof Vector) {
                typeName = " Vector";
            }else if(v instanceof ArrayList){
                typeName = " ArrayList";
            } else if(v instanceof LinkedList){
                typeName = " LinkedList";
            }
            System.out.println(name + typeName + " size is " + v.size());
            try {
                Thread.sleep(10);
            } catch(InterruptedException e) {
                System.out.println(e.getMessage());
            }
            count--;
        }
    }
}
